import com from './addon/com'
var components = com([
	'HeaderLayout',
	'HeaderDock',
	'LayoutMain',
	'LayoutBox',
	'Btn',
	'Icon',
	'FormGroup',
	'FormInput',
	'FormSelect',
	'FormUpload',
	'FormCode',
	'GridRow'
])

//
new Vue({
	el: '#app',
	components: components,
	created: function(){
		var _this = this;
		for(var key=0; key<CATE.length; key++){
			this.categories.push({
				val: CATE[key].id,
				text: CATE[key].name
			});
		}

		for(var key = 0; key< VAR.length; key++){
			this.varietiesall.push({
				val: VAR[key].id,
				text: VAR[key].name,
				cid: VAR[key].categoryid
			});
		}

	 
	},
	data (){
		return {
			 uploadname: '添加照片',
			 categories: [],
			 varietiesall: [],
			 varieties: []
		}
	},
	methods: {
		submit: function(){
			this.$broadcast('form-verify', '');
 
			if(this.$refs.form.pass){
				var params = this.$refs.form.data;
				params.image = $('#img').val();
				 
				$.ajax({
					url: SITE_URL + '/admin/save',
					type:'post',
					data: params,
					success: function(data){
						if(!data.code){
							alert(data.msg);
						} else {
							alert('添加成功');
							window.location.reload();
						}
					},
					error: function(){
						alert('系统错误')
					}
				});
			} else {
				//this.$refs.myform.setNotice('账号密码不正确')
				console.log('data wrong')
			}

		},
		cateChange: function(obj){
			//this.categories = [];
			var vari = [];
			for(var key=0; key<VAR.length; key++){
				if(VAR[key].categoryid == obj.val){
					vari.push({
						val: VAR[key].id,
						text: VAR[key].name
					})
				}
			}
			//console.log(vari.toString());
			this.varieties = vari;
		},
 		uploadchange: function(file){
 			this.uploadname = '重新添加照片';
 			
 			lrz(file.files[0], {
				width: 300
			})
	        .then(function (rst) {
	            // 处理成功会执行
	           // console.log(rst);
	            var img = new Image();
	            img.src = rst.base64;

	            img.onload = function () {
	            	document.getElementById('picpreview').innerHTML = '';
	                document.getElementById('picpreview').appendChild(img);
	            };

	            var xhr = new XMLHttpRequest();
	            xhr.open('POST', SITE_URL+'/up/up_pic');

	            xhr.onload = function () {
	                if (xhr.status === 200) {
	                    //alert(xhr.responseText);
	                    $('#img').val(xhr.responseText);
	                } else {
	                    alert('网络异常，请重试');
	                }
	            };

	            xhr.onerror = function () {
	                
	            };

	            xhr.upload.onprogress = function (e) {
	                // 上传进度
	                var percentComplete = ((e.loaded / e.total) || 0) * 100;
	            };

	            // 添加参数
	            rst.formData.append('fileLen', rst.fileLen);
	           // rst.formData.append('filedata', rst.file);

	            // 触发上传
	            xhr.send(rst.formData);

	            return rst;
	        })
	        .catch(function (err) {
	           	alert(err)
	        })
	        .always(function () {
	            // 不管是成功失败，都会执行s
	        });
 		}
	}
})